Python GUI SDK
Introduction
The application includes a Python Software Development Kit [SDK] layer for creating GUI components. At present, the SDK is limited to the creation of custom tools, which are GUI windows that permit the application user to drive the operation of the application with interactive input. The SDK is fully documented in the form of a user manual.
Overview
The SDK consists of one Python module, apex_sdk, which defines four classes, one decorator function, and two global functions. Together these are used to define the contents and operation of a custom tool.
The SDK is integrated with and uses Microsoft .NET classes. Authoring scripts will require some familiarity with commonly used .NET classes and Windows Presentation Foundation [WPF] controls. Note that use of generic .NET classes requires a special Python syntax. For example, List[System.String]() constructs a List of String.
The SDK uses Python for .NET to integrate with Microsoft .NET. The documentation provided for Python for .NET can assist you in converting C# or Visual Basic for .NET syntax for use in Python when using .NET types.
Script Samples
The application delivers two sample custom tool scripts. The installer places these tools in %USERPROFILE%\Documents\MSC_Apex Workspace\UserDefinedTools. Each tool consists of an SDK script (e.g. CustomTool1_sdk.py) and one or more scripts that use the MSC Apex Scripting API [API] (e.g. CustomTool1Geometry_api.py). Before writing your own scripts, it is useful to examine the contents of these scripts in order to understand how they are organized and work. You should follow the pattern of separating scripts that use the SDK from those that use the API in order to avoid conflicts and UI stalling (black screen).
Here are the steps necessary to activate the sample tools in order to see how they work:
- Start an Apex interactive session.
- Select Options, then Application Settings from the Apex menu bar.
- Select Scripting And Customization from the left hand side of the Application Settings dialog.
- Check the Enable Custom Tool Palette from the right hand side of the Application Settings dialog.
- Click OK from the Application Settings dialog.
- Click the Custom Tool Palette button from the right-hand Apex tool menu bar. The Custom Tool Palette tool menu will appear with three custom tool buttons defined in the custom tool group named User Defined Tools.
- Click on any of the tool buttons to display the dialog defined by the custom tool. Interact with the dialog to observe how it works. Note that the CustomTool3 button is associated to an API script, and not to an SDK script. It demonstrates that an API script can be called as either a macro or in response to an event defined in an SDK script.
Script Requirements
An SDK script conforms to the following requirements:
- The script must define a global function named getUIContent. This function must take no arguments and create and return a tool property container, which is a single instance of the class apex_sdk.ToolPropertyContainer.
- The script must create the UI layout for the custom tool from WPF controls. Only one top-level control may be created, and it must be a layout control, such as a grid. Other controls may be created as children, possibly nested, of the layout. The top level control must be assigned to the ToolPropertyContent attribute of the tool property container.
- WPF controls define events and permit functions to be assigned to them to handle the events. An SDK script can add a global function to any control event property to use it as an event handler. This function must take two parameters and return no values. The first parameter is the sender of the event and is an instance of System.Object. The second parameter contains state information and event data associated with the event and is an instance of System.Windows.RoutedEventArgs. Decorate this function with apex_sdk.errorhandler.
- The script must define a global function to handle the click event of the tool Apply button if apex_sdk.ToolPropertyContainer.IsAppliedEnabled is True. This function must take no arguments and return no value. It must be wrapped in a System.Action delegate instance, which must itself be wrapped in an apex_sdk.ActionCommand class instance, and assigned as the value of apex_sdk.ToolPropertyContainer.AppliedCommand.
- The script must define a global function to handle the click event of the tool Exit button. This function must take no arguments and return no value. It must be wrapped in a System.Action delegate instance, which must itself be wrapped in an apex_sdk.ActionCommand class instance, and assigned as the value of apex_sdk.ToolPropertyContainer.ExitCommand.
- The script may decide to show and fill a custom tool pick filter that restricts user selections to objects in the filter. Add apex_sdk.PickFilterTypes attributes to a .NET List of String and pass it to the method apex_sdk.PickFilterRegistration.RegisterTool to define the contents of the pick filter. You must also pass the name of your custom tool to this method. In addition, set the ShowPickChoice and IsCustomTool attributes of the tool property container to True.
Show/Hide Custom Tools
Step-by-step instructions to display and hide your custom tool:
Click on the Custom Tool Palette menu.
.png)
Right-click on the Custom Tool Palette tool menu, then select Hide Group or Show All Tools In Group from the context menu to hide or display all custom tools.
.png)
To hide a specific custom tool, right-click on the tool in the context menu and select Hide Tool.
.png)
SDK Contents
These are the contents of the apex_sdk module, linked to their location in the Reference Manual.
Classes
- Class apex_sdk.ToolPropertyContainer
- Class apex_sdk.ActionCommand
- Class apex_sdk.PickFilterTypes
- Class apex_sdk.PickFilterRegistration
- Class apex_sdk.DataDescriptor
- Class apex_sdk.ExpandableStageButton
Decorators
- Decorator apex_sdk.errorhandler
Functions
- Function runScript(script)
- Function runScriptFunction(script, function, dictionary)
- Log in to post comments
